package com.zoran.study.spring.controller;


import com.zoran.study.spring.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.WebAsyncTask;

import java.util.concurrent.Callable;

/**
 * UserController
 *
 * @author zouxm
 * @date 2018/6/22
 */
@RestController
@RequestMapping("/async")
@Slf4j
public class AsyncRequestController {

    @Autowired
    private TaskExecutor taskExecutor;

    @RequestMapping(value = "/test01", produces = {MediaType.APPLICATION_JSON_VALUE})
    public Callable<User> test01() {
        System.out.println(Thread.currentThread().getName());
        return () -> {
            System.out.println(Thread.currentThread().getName());
            Thread.sleep(10000L);
            return new User("zouxm");
        };
    }

    @RequestMapping(value = "/test02", produces = {MediaType.APPLICATION_JSON_VALUE})
    public WebAsyncTask<User> test02() {
        System.out.println(Thread.currentThread().getName());
        WebAsyncTask<User> asyncTask = new WebAsyncTask<>(5000L, (AsyncTaskExecutor) taskExecutor, () -> {
            System.out.println(Thread.currentThread().getName());
            return new User("zouxm");
        });
//        asyncTask.onTimeout();
        return asyncTask;
    }


}
